package com.persondemo.videoappliction.ui.duanzi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.flyco.tablayout.SlidingTabLayout;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.ui.adapter.FragmentVpAdapter;
import com.persondemo.videoappliction.ui.base.BaseFragment;
import com.persondemo.videoappliction.ui.publish.PubLishActivity;
import com.persondemo.videoappliction.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * Created by Administrator on 2018/4/18.
 */

public class DuanziFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.vp)
    CustomViewPager vp;
    Unbinder unbinder1;
    @BindView(R.id.home_tab)
    SlidingTabLayout homeTab;
    @BindView(R.id.tougao_text)
    Button tougaoText;
    private List<String> tablist = new ArrayList<>();
    private FragmentVpAdapter fragmentVpAdapter;
    public static DuanziFragment newInstance() {
        Bundle args = new Bundle();
        DuanziFragment fragment = new DuanziFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_fragment_duanzi;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {

    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
    }

    @Override
    public void initData() {
        tablist.clear();
        tablist.add("全部");
        tablist.add("段子");
        tablist.add("视频");
        tablist.add("图片");
        tablist.add("妹子图");
        tablist.add("星闻");
        fragmentVpAdapter = new FragmentVpAdapter(getChildFragmentManager(), tablist);
        vp.setAdapter(fragmentVpAdapter);
        homeTab.setViewPager(vp);
        vp.setOffscreenPageLimit(1);
        vp.setCurrentItem(0, false);
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.i("lidaming", "视频播放postion=" + position);
                if (position != 2 || position != 0) {
                    JCVideoPlayer.releaseAllVideos();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }


    @OnClick(R.id.tougao_text)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), PubLishActivity.class));
        getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }
}
